//################################################################################################################################//
//##                                                                                                                            ##//
//## This Page is implement for post message in the particular topic                                                            ##//
//##                                                                                                                            ##//
//## Author   : Manish Prajapati                                                                                                ##//
//## Dated    : 18-03-2006                                                                                                      ##//
//##                                                                                                                            ##//
//################################################################################################################################//
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
public partial class includes_postMessage : System.Web.UI.UserControl
{
    // creating an instance of manager class
    Manager manager = new Manager(true,"ConnectionString");
    int _TopicId=0;
    string _postFor = "";
    /// <summary>
    /// set and get TopicId in _TopicId
    /// </summary>
    public int TopicId
    {
        set{_TopicId = value;}
        get{return _TopicId;}
    }
   /// <summary>
   /// Set and get last page action means user/doctor come from which page.
   /// </summary>
    public string PostFor
    {
        set { _postFor = value; }
        get { return _postFor; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Visible = false;
        localFunction lFunction = new localFunction();
        if (ViewState["_TopicId"] != null)
        {
            this.TopicId = Convert.ToInt32(ViewState["_TopicId"]);
        }
        else
        {
            //store TopicId in ViewState
            ViewState["_TopicId"] = this.TopicId;
        }
        //Hide Control if Doctor have Login and doctor come form BLog Board
        if (lFunction.checkDoctorLogin(false) && this.PostFor == "BlMessage")
        {            
            this.Visible = true;
        }
        else if (lFunction.checkDoctorLogin(false) && this.PostFor == "dentist")
        {
            this.Visible = true;
        }
        else if (lFunction.checkDoctorLogin(false) && this.PostFor == "drMessage")
        {
            this.Visible = true;
        }
        else if (lFunction.checkDentistLogin(false) && this.PostFor == "dentist")
        {
            this.Visible = true;
        }
        //Show Control if User Login
        else if (lFunction.checkUserLogin(false))
        {
            this.Visible = true;
        }        
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //Insert message in tbl_Messages
        int userType = 0;
        if ((Session["userType"].ToString() == "Doctor") && ((Convert.ToString(Session["DoctorType"]).Length==0)))
            userType = 2;
        else if (Session["userType"].ToString() == "SiteUser")
            userType = 3;
        else if ((Session["userType"].ToString() == "Doctor") && (Convert.ToString(Session["DoctorType"])== "Dentist"))
            userType = 4;
        string SQL = "";
        string Message = txtMessage.Text.Replace("'", "''").Trim();
        if (this.PostFor=="drMessage")
        {
            SQL = " INSERT INTO tbl_doctorMessage(DoctorID,UserID,Description,UserType) Values ";
            SQL = SQL + " ('" + this.TopicId + "','" + Convert.ToInt32(Session["userID"].ToString()) + "','" + Message + "','" + userType + "')";
        }
        else if (this.PostFor == "BlMessage")
        {
            SQL = " INSERT INTO tbl_Messages(TopicId,UserID,Description,UserType) Values ";
            SQL = SQL + " ('" + this.TopicId + "','" + Convert.ToInt32(Session["userID"].ToString()) + "','" + Message + "','" + userType + "')";
        }
        //This code is Added by Vinayak Singh Rathore post message for dentist
        else if (this.PostFor == "dentist")
        {
            SQL = " INSERT INTO tbl_dentistMessage(DentistID,UserID,Description,UserType) Values ";
            SQL = SQL + " ('" + this.TopicId + "','" + Convert.ToInt32(Session["userID"].ToString()) + "','" + Message + "','" + userType + "')";
        }
        if (Message.Trim()!= "")
        {
            if (txtMessage.Text.Length < 15000)
            {
                manager.ExecuteUpdate(SQL);
            }
            else
            {
                lblerr.Visible = true;
                lblerr.Text = "<FONT COLOR=RED>Post Message Maximum Length is Only 2000 Characters</FONT>";
            }
        }
        txtMessage.Text = "";

   }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtMessage.Text = "";
    }
}
